package vg.modules.scaling;


import java.util.Observable;

import javax.swing.JComponent;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

import vg.modules.scaling.components.ZoomBox;
import vg.modules.scaling.components.ZoomIn;
import vg.modules.scaling.components.ZoomOut;
import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.graph_view_manager.interfaces.tab.ATab;
import vg.services.plugin_manager.event_and_request.event.AUIEvent;
import vg.services.plugin_manager.event_and_request.event.UIEventChangeTab;
import vg.services.user_interface_manager.interfaces.AUserInterface;
import vg.services.user_interface_manager.interfaces.AUserInterfaceElement;

public class ZoomPanel extends AUserInterfaceElement {
	// Mutex
	private final Object general_mutex = new Object();
	
	// Main components
	private final ZoomIn zoomIn;
	private final ZoomOut zoomOut;
	private final ZoomBox zoomBox;
	private final JToolBar toolBar;
	
	// Main data
	private final AUserInterface userInterface;
	/**
	 * Constructor.
	 */
	public ZoomPanel(final AUserInterface userInterface) {
		super("Zoom panel", null);
		
		// initialize data
		this.userInterface = userInterface;
		
		// initialize components
		zoomIn = new ZoomIn();
		zoomOut = new ZoomOut();
		zoomBox = new ZoomBox();
		//toolBar = new JPanel(new GridBagLayout());
		toolBar = new JToolBar();
		toolBar.setFloatable(false);
		
		toolBar.add(this.zoomBox.getView());
		toolBar.add(this.zoomIn.getView());
		toolBar.add(this.zoomOut.getView());
				
		userInterface.addInstrument(this);
	}
	
	public void update(Observable o, Object arg) {
		synchronized (this.general_mutex) {
			if(arg instanceof AUIEvent) {
				AUIEvent event = (AUIEvent)arg;
				switch (event.getType()) {
					case DEF_CHANGE_UI_STYLE: {
						SwingUtilities.invokeLater(new Runnable() {
							public void run() {
								synchronized (general_mutex) {
									SwingUtilities.updateComponentTreeUI(toolBar);
								}
							}
						});
						break;
					}
					
					case DEF_CHANGE_TAB: {
						UIEventChangeTab bufEvent = (UIEventChangeTab)event;
						ATab tab = bufEvent.getTab();
						if (tab != null) {
							switch (tab.getTabType()) {
								case ATab.DEF_GRAPH_VIEW_TAB_TYPE: {
									AGraphView igv = (AGraphView)tab;
									zoomIn.changeView(igv);
									zoomOut.changeView(igv);
									zoomBox.changeView(igv);
									break;
								}							
							}
						}
						break;
					}
					
					default: break;
				}
			}
		}
	}
	
	public JComponent getView() {
		return toolBar;
	}
}
